package com.sgb.goods.viewmodel;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.StorageManageEntity;
import com.sgb.goods.entity.inquiry.MapLongAndLatEntity;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;

import java.util.List;
import java.util.Objects;

/**
 * 描述：商品-添加仓储
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.12.01
 */
public class AddStorageViewModel extends BaseViewModel {

    public ObservableField<StorageManageEntity> storageManEntity = new ObservableField<>(new StorageManageEntity());
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> addressEntityLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> onFinishActivity = new MutableLiveData<>();
    public MutableLiveData<MapLongAndLatEntity> andLatEntityMutableLiveData = new MutableLiveData<>();

    /**
     * 所有地址选项
     */
    public void getAddressList() {
        showLoading();
        GoodsNet.getInstance().getGoodsApi().getCityList().enqueue(new GoodsCallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data.getData() != null) {
                    addressEntityLiveData.postValue(data.getData());
                }
            }
        });
    }

    public void save() {
        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID, "");
        Objects.requireNonNull(storageManEntity.get()).setCompanyId(companyId);
        showLoading();
        GoodsNet.getInstance().getGoodsApi().saveStorage(storageManEntity.get()).enqueue(new GoodsCallBack<BaseEntity<Object>>() {

            @Override
            public void onSuccess(BaseEntity<Object> response) {
                onFinishActivity.postValue(true);
            }
        });
    }

    /**
     * 获取经纬度信息
     *
     * @param province
     * @param city
     * @param district
     */
    public void getLocationLL(String province, String city, String district) {
        showLoading();
        String areaCode = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
        GoodsNet.getInstance().getGoodsApi().getLocationLL(areaCode, province, city, district).enqueue(new ICallBack<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onSuccess(BaseEntity<MapLongAndLatEntity> data) {
                andLatEntityMutableLiveData.postValue(data.getData());
            }
        });
    }

}
